package com.example.arithmeticleetcode.leetcode.September;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-09 09:51
 **/
public class Demo08 {

    public static void main(String[] args) {
        int[] A1 = {1}; //0
        int[] A2 = {0 ,10}; //6
        int[] A3 = {1, 3, 6}; //0
        System.out.println(smallestRangeI(A1, 0));
        System.out.println(smallestRangeI(A2, 2));
        System.out.println(smallestRangeI(A3, 3));

        System.out.println(smallestRangeI2(A1, 0));
        System.out.println(smallestRangeI2(A2, 2));
        System.out.println(smallestRangeI2(A3, 3));

        System.out.println("==============================");
        Long c = 1L;
        new Demo08().changeLong(c);
        System.out.println(c);
    }

    public void changeLong(Long creater) {
        creater = 2L;
    }

    public static int smallestRangeI(int[] A, int K) {
        Arrays.sort(A);
        int result = A[A.length - 1] - A[0] - 2 * K;
        return result > 0 ? result : 0;
    }

    public static int smallestRangeI2(int[] A, int K) {

        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0, len = A.length; i < len; i++) {
            int temp = A[i];
            if (temp > max) {
                max = temp;
            }
            if (temp < min) {
                min = temp;
            }
        }
        int result = max - min - 2 * K;
        return result > 0 ? result : 0;
    }
}
